using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Voodoo.Sauce.Internal.Utils
{
	public static class AssembliesUtils
	{
		public static List<T> InstantiateInterfaceImplementations<T>() where T : class
		{
			Type interfaceType = typeof(T);
			return (from type in AppDomain.CurrentDomain.GetAssemblies().SelectMany((Assembly assembly) => assembly.GetTypes())
				where interfaceType.IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract
				select (T)Activator.CreateInstance(type)).ToList();
		}

		public static List<Type> GetTypes(Type toGetType)
		{
			List<Type> list = (from type in AppDomain.CurrentDomain.GetAssemblies().SelectMany((Assembly assembly) => assembly.GetTypes())
				where toGetType.IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract
				select type).ToList();
			list.Sort((Type x, Type y) => string.Compare(x.Name, y.Name, StringComparison.Ordinal));
			return list;
		}
	}
}
